<?php
/**
 * ClassName: treatment_services
 * Description: 给病人开的治疗服务controller
 * @Author 周昌
 * @Create 2023/11/17 9:13
 */

namespace app;


use epii\server\Args;
use model\TreatmentModel;
use model\TreatmentServicesModel;
use model\WaitModel;
use service\admin\TreatmentCateService;
use service\admin\TreatmentService;
use service\admin\HospitalService;
use service\admin\TreatmentServicesService;
use service\admin\WaitService;

class treatment_services extends base
{
    public function init()
    {
        parent::init();
        $this->accessDenied();
    }
    public function index()
    {
        if($this->adminInfo['role']==1){
            $this->assign('hospitals',HospitalService::getSelect());
        }
        $this->adminUiDisplay();
    }

    public function ajax_data()
    {
        $map = [];
        //登录用户权限判断
        $hospital_id=$this->adminInfo['hospital_id'];
        if($hospital_id){
            $map[]=['hospital_id','=',$hospital_id];
        }

        $name = Args::params('name');
        if ($name) {
            $map[] = ['name', "LIKE", '%' . $name . '%'];
        }

        $hospital_id = Args::params('hospital_id');
        if ($hospital_id) {
            $map[] = ['hospital_id', "=", $hospital_id];
        }
        echo TreatmentService::getAjaxData($map);
    }
    //问诊(病历记录) 治疗项保存
    public function add_edit()
    {
        $case_id = Args::params('case_id');
        if (Args::isPost()) {
            $data = Args::params('r');
            //问诊排号 结束或取消 禁止修改数据
            $wait_info=WaitModel::getInfo(['case_id'=>$case_id,'type'=>0]);
            if($wait_info['status']>WaitService::$wait_status_index['progress']){
                $this->error('该问诊已结束或已取消无法保存');
            }

            $res=TreatmentServicesService::addEdit($case_id,$data);

            $res['code']?$this->success($res['msg']):$this->error($res['msg']);
        }
        $this->_as_info=TreatmentServicesService::getServicesList(['case_id'=>$case_id]);
        $this->adminUiDisplay();
    }
    //病历-取消治疗服务
    public function cancel(){
        $id=Args::params('id/d/1','id,不能为空');
        $status=Args::params('status/d');
        //判断是不是未操作状态
        if ($status!==TreatmentServicesService::$services_status_index['no']){
            $this->error('只能取消未操作的服务');
        }
        $res=TreatmentServicesService::status($id,TreatmentServicesService::$services_status_index['cancel']);
        if($res['code']){
            $this->success('成功','refresh');
        }
        $this->error();
    }

    /**
     * @return array|false|string* 删除
     */
    public function del()
    {
        $id = Args::params('id');
        //todo 判断治疗项目下是否已经有订单，无则可以删除
        $res = TreatmentService::del($id);
        if ($res['code']) {
            $this->success($res['msg'],'refresh');
        } else {
            $this->error($res['msg']);
        }
    }
    // 问诊 通过 分类id 获取 治疗项目列表
    public function getTreatmentList()
    {
        $treatment_cate_id=Args::params('treatment_cate_id/d');
        $list=TreatmentModel::getList(['treatment_cate_id'=>$treatment_cate_id],'id,name,price');
        exit(json_encode($list));
    }


}